package com.controller;

import com.pojo.Media;
import com.pojo.Result;
import com.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

    @PostMapping
    public Result<Media> createMedia(@RequestBody Media media) {
        return mediaService.createMedia(media);
    }

    @PutMapping("/{id}")
    public Result<Void> updateMedia(@PathVariable Integer id, @RequestBody Media media) {
        media.setId(id);
        return mediaService.updateMedia(media);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteMedia(@PathVariable Integer id) {
        return mediaService.deleteMedia(id);
    }

    @DeleteMapping("/note/{noteId}")
    public Result<Void> deleteMediaByNoteId(@PathVariable Integer noteId) {
        return mediaService.deleteMediaByNoteId(noteId);
    }

    @GetMapping("/{id}")
    public Result<Media> getMediaById(@PathVariable Integer id) {
        return mediaService.getMediaById(id);
    }

    @GetMapping("/note/{noteId}")
    public Result<List<Media>> getMediaByNoteId(@PathVariable Integer noteId) {
        return mediaService.getMediaByNoteId(noteId);
    }

    @GetMapping("/type/{mediaType}")
    public Result<List<Media>> getMediaByType(@PathVariable String mediaType) {
        return mediaService.getMediaByType(mediaType);
    }

    @PatchMapping("/{id}/order")
    public Result<Void> updateMediaOrder(
            @PathVariable Integer id,
            @RequestParam Integer orderIndex) {
        return mediaService.updateMediaOrder(id, orderIndex);
    }
}